package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.form.OrderQueryForm;
import com.springboot.cloud.demos.entity.param.OrderQueryParam;
import com.springboot.cloud.demos.entity.param.OrderQueryParam;
import com.springboot.cloud.demos.entity.po.Order;
import com.springboot.cloud.demos.entity.po.Order;
import com.springboot.cloud.demos.producer.dao.OrderMapper;
import com.springboot.cloud.demos.producer.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RefreshScope
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Value("${producer.order:123}")
    private String value;

    @Override
    @CacheEvict(value = "order",allEntries=true)
    public boolean add(Order order) {
        return this.save(order);
    }

    @Override
    @CacheEvict(value = "order", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "order", key = "#root.targetClass+'-'+#order.id")
    public boolean update(Order order) {
        return this.updateById(order);
    }

    @Override
    @Cacheable(value = "order", key = "#root.targetClass+'-'+#id")
    public Order get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    public List query(OrderQueryParam orderQueryParam) {
        return this.getBaseMapper().query(orderQueryParam.getUserId());
    }

    @Override
    public IPage page(OrderQueryForm orderQueryForm) {
        OrderQueryParam orderQueryParam = orderQueryForm.toParam(OrderQueryParam.class);
        Page page = new Page<>(orderQueryForm.getCurrent(), orderQueryForm.getSize());
        QueryWrapper<Order> queryWrapper = orderQueryParam.build();
        if (StringUtils.isNotEmpty(orderQueryParam.getUserId())) {
            queryWrapper.eq("mobile", orderQueryParam.getMobile());
        }
        if (StringUtils.isNotEmpty(orderQueryParam.getConsignee())) {
            queryWrapper.eq("consignee", orderQueryParam.getConsignee());
        }
        if (StringUtils.isNotEmpty(orderQueryParam.getLevelName())) {
            queryWrapper.eq("levelName", orderQueryParam.getLevelName());
        }
        if (StringUtils.isNotEmpty(orderQueryParam.getLevelName())) {
            queryWrapper.eq("levelName", orderQueryParam.getLevelName());
        }

        IPage<Map<String, Object>> iPage = this.getBaseMapper().selectMapsPage(page, queryWrapper);
        return iPage;
    }

}
